<div id="dialog-fileSelector" class="form-horizontal">
    <div class="form-group">
    	<ul id="ztreeForFileSelector" class="ztree" style="margin-bottom: 120px;"></ul>
    </div>
	<div class="modal-footer">
	    <div class="form-group">
		    <div class="col-sm-12 text-right">
		        <button  type="button" class="btn btnPrimary" onclick="FileSelector.doSelect();" style="width: 100px;height: 42px;">确定</button>
		        <button  type="button" class="btn btnPrimary" onclick="FileSelector.cancelSelect();" style="width: 100px;height: 42px;">取消</button>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
var FileSelector = (function () {
	var _config;
	var _localDiskPath = "";
	var _reposId = "";
	var _repos;
	var _serverId = "";
	var _targetReposId = "";
	var _targetDiskPath = "";
	var _listType = ""; //默认不指定
	var _docInfo;
	var _menu;
	var _selectedNode;
	
    function zTreeBeforeAsync(treeId, treeNode) {
    	console.log("zTreeBeforeAsync treeId:"+ treeId, treeNode);
    }

    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    	console.log("zTreeOnAsyncSuccess treeId:" + treeId);
    }

	//This function was used to get the rightClick treeNode,it will be used for contextjs
	function zTreeBeforeRightClick(treeId, treeNode) {
		_selectedNode = treeNode;
    	return true;
	};

	//This function will replace all righot click, so i did not use it
	function zTreeOnRightClick(event, treeId, treeNode) {
    	//alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
	};
	
	//zTree callback definitions
    function zTreeOnClick(event, treeId, treeNode) 
    {
    	console.log("zTreeOnClick treeId:"+ treeId, treeNode);
    	_selectedNode = treeNode;
    }
      
    function zTreeOnDbClick(event, treeId, treeNode) 
    {
    	console.log("zTreeOnDbClick treeId:"+ treeId, treeNode);
    	_selectedNode = treeNode;

    	doSelect();
    }
    
    function formatDocList(docList)
    {
    	if(!docList)
		{
			return docList;
		}
	
    	//遍历jason_arry, convert the node type to isParent flag
      	for(var i=0; i<docList.length; i++)
      	{
           var jsonObj = docList[i];
           jsonObj.id = jsonObj.docId;
           jsonObj.pId = jsonObj.pid != -1? jsonObj.pid : "root",
           jsonObj.isParent = jsonObj.type == 1? false: true;
           jsonObj.rootPath = "";
           jsonObj.rootName = "";
           //设置用户自定义文件图标（必须在standardStyle中有定义）
           if(jsonObj.type == 1)
           {
        	   var iconType = getDiyFileIconType(jsonObj.name);
        	   if(iconType && iconType != "")
        	   {
        		   jsonObj.iconSkin = iconType;
        	   }
           }
      	}
    }
    
  	function asyncDataFilter(treeId, parentNode, responseData) 
  	{
		console.log("asyncDataFilter");
		var docList = responseData.data;
		formatDocList(docList);		
		console.log(docList);
		return docList;
	}
	
	function zTreeInit(data, node) {
	    console.log("zTreeInit");
        //设置异步加载的参数
        var async =  {
    		enable : true,//设置 zTree 是否开启异步加载模式
            url : "/DocSystem/Doc/getSubDocList.do",
            type : "post",
    		autoParam : ["path","name"],//异步加载时需要自动提交父节点属性的参数
    		otherParam:{
    			"storageType" : _storageType,
    			"localDiskPath" : _localDiskPath,
    			"reposId" : _reposId,
				"serverId"	: _serverId,
				"targetReposId" : _targetReposId,
		    	"targetDiskPath" : _targetDiskPath,
				"listType": _listType,	//3: LocalEntryAndRemoteStorage
    		},
    		dataFilter: asyncDataFilter,
		};
        
    	//zTree's setting
    	var setting = {
    	   	//可编辑功能设置
    		edit: {},
               //zTree数据格式
    	   	data: {
    	   			//使用简单数据模式
    	            simpleData: {
    	                enable: true,
    	            },
    	            //不允许修改leaf node and parent node的isParent属性
    	            keep: {
    	            	leaf: true,
    	            	parent: true,
    	            },
    	    },
    	    //zTree各种操作的回调函数定义
    	    callback: {
    	            beforeAsync: zTreeBeforeAsync, //异步加载前的回调函数， 可以用来判断是否需要异步加载
                    onAsyncSuccess: zTreeOnAsyncSuccess, //异步加载完成后的回调
    	            onClick: zTreeOnClick,
    	            onDblClick: zTreeOnDbClick,
    	            beforeRightClick: zTreeBeforeRightClick,
    	            onRightClick: zTreeOnRightClick, //定义该回调将会屏蔽系统右击事件
    	    },
    	};
    	
    	setting.async = async;
	    
    	var zTree = $.fn.zTree.init($("#ztreeForFileSelector"), setting, data);
	    if(node && node.docId)	//try to select the node
	    {
	    	var treeNode = zTree.getNodeByParam("id", node.docId);
    		if(treeNode && treeNode != null)
    		{
    		 	zTree.selectNode(treeNode);
    			_selectedNode = treeNode;
    		}
	    }
	    else
	    {
	    	var rootNode = zTree.getNodeByParam("id", 0);
			if(rootNode)
			{
				zTree.expandNode(rootNode, true, false, true); //展开根目录
			}
	    }
	}
	
	function buildRootDoc(repos)
	{
		var node = {};
		node.vid = _reposId;
		node.id = 0;
		node.docId = 0;	//docId same to id
		node.pid = -1;
		node.path = "";
		node.name = "";
		node.type = 2;	//rootDoc must be folder
		return node;
	}
	
	function getInitMenu(node)
   	{
		console.log("getInitMenu node:", node);
		var docId = 0;
		var path = "";
		var name = "";
		if(node)
		{
			path = node.path;
			name = node.name;
		}
		
		$.ajax({
		    url : "/DocSystem/Doc/getInitSubDocList.do",
		    type : "post",
		    dataType : "json",
		    data : {
		        storageType: _storageType,
		        localDiskPath: _localDiskPath,
		        reposId  : _reposId,
		    	serverId : _serverId,
		    	targetReposId: _targetReposId,
		    	targetDiskPath: _targetDiskPath,
		    	path: path,
		    	name: name,
		    	listType : 3,    //LocalEntryAndRemoteStorage
		    },
		    success : function (ret) {
		        	if( "ok" == ret.status ){
		        		console.log("getInitMenu() ret",ret);

		        		var docList = ret.data || [];
		        		InitMenu(docList, node);
		            }
		            else
		           	{
						showErrorMessage("获取目录失败:" + ret.msgInfo);
		           	}
		        },
		        error : function () {
		            showErrorMessage("获取目录失败:服务器异常");
		        }
		    });
	}
	
	function InitMenu(menu, node)
	{
		var rootDoc = buildRootDoc(_repos);
		menu.push(rootDoc);
		formatDocList(menu);
	    zTreeInit(menu, node);
	}
	
	function fileSelectorPageInit(config)
	{
		//初始目录
		console.log("fileSelectorPageInit() config:",config);
		_config = config || {};
		_storageType = _config.storageType;
		
		if(_config.localDiskPath)
		{
			_localDiskPath = _config.localDiskPath;	
		}
		
		if(_config.reposId)
		{
			_reposId = _config.reposId;	
			_repos = config.repos;
		}
		
		if(_config.serverId)
		{
			_serverId = _config.serverId;
			if(_config.targetDiskPath)
			{
				_targetDiskPath = _config.targetDiskPath;
			}
			if(_config.targetReposId)
			{
				_targetReposId = _config.targetReposId;	
			}
		}
		
		if(_config.listType)
		{
			_listType = _config.listType;
		}
		
		_docInfo = config.doc;
		
		if(_config.menu)
		{
			InitMenu(_config.menu, _docInfo);
		}
		else
		{
			getInitMenu(_docInfo);
		}
	}
	
	function closeFileSelectorDialog()
	{
		closeBootstrapDialog("fileSelector");
	}

	function doSelect()
	{
		console.log("doSelect() _selectedNode:",_selectedNode);

		//callbck
		_config.onSelect && _config.onSelect(_selectedNode);
		
		closeFileSelectorDialog();	
	}
	
	function cancelSelect()
	{
		closeFileSelectorDialog();
	}
	
	//开放给外部的调用接口
    return {
    	fileSelectorPageInit: function(config){
    		fileSelectorPageInit(config);
		},
		doSelect: function(){
			doSelect();
		},		
		cancelSelect: function(){
			cancelSelect();
		},    };
})();
</script>

